package com.umakr.ax.security.core.handler;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author gx
 * @since 2017/5/15
 */
public class LoginFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    private static final String ACCEPT_CONTAINS = "application/json";

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        String accept = request.getHeader("accept");
        if(accept!=null&&accept.contains(ACCEPT_CONTAINS)){
            response.sendError(403,exception.getMessage());
        }else{
            super.onAuthenticationFailure(request, response, exception);
        }
    }
}
